# how to do it? generate random sessions.
from title_tweaker import minmax
import requests

# if does not have session_id, then randomly generate one.
# what is the location? and what is the api?
# forget about it.
Turing_API_key = "64c88489ad7f432591d702ec1334dedc" 
Turing_API_address = "http://www.tuling123.com/openapi/api"
_min, _max = 0,25535
class chatSession:
    def __init__(self,key=None,location=None,userid=None):
        if userid == None:
            pass
        else:
            assert type(userid) == int
            assert userid>_min
            assert userid<_max
        self.key = Turing_API_key if key is None else key
        self.location = "北京市中关村" if location is None else location
        self.userid = round(minmax(_min,_max)+1) if userid is None else userid
    def chat(self,text):
        assert type(text) == str
        assert len(text)>0
        # no fucking more.
        r = requests.post(
            Turing_API_address, 
            json = {
            "key": self.key,
            "info": text, 
            "loc": self.location, 
            "userid": str(self.userid)
            })
#        print(r.json())
        # code must be 100000.
        rj = r.json()
#        print(rj)
        assert rj["code"] == 100000
        return rj["text"]
